package com.lq.schoolshop.dao;

import com.lq.schoolshop.pojo.Expand.UserExamp;
import com.lq.schoolshop.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * @Author
 * @Date2021/1/5 22:12
 * @Version V1.0
 *  用户实体类
 **/
public interface UserMapper {
    /**
     * 根据ID查询全部的用户信息
     * @param id
     * @return
     */
    @Select("SELECT NAME,phone,address,icon FROM USER WHERE id = #{id} ")
    UserExamp showall(int id);

    /**
     * 根据名字查询头像地址
     * @param username
     * @return
     */
    @Select("SELECT icon FROM USER WHERE username = #{username}")
    String getlogoImg(@Param("username") String username);

    /**
     * 根据ID查询头像地址
     * @param id
     * @return
     */
    @Select("SELECT icon FROM USER WHERE id = #{id}")
    String getlogoImgById(@Param("id")int id);

    /**
     * 根据ID修改头像
     * @return
     */
    @Update("UPDATE USER SET icon = #{icon} WHERE id = #{id} ")
    int updateImg(@Param("id") int id,@Param("icon") String icon);

    /**
     * 修改用户所有的信息
     * @param name
     * @param phone
     * @param address
     * @param id
     * @return
     */
    @Update("UPDATE USER SET NAME=#{name} , phone = #{phone} ,address = #{address} WHERE id=#{id} ")
    int updateuserinfo(@Param("name") String name,@Param("phone") String phone,@Param("address") String address,@Param("id") int id);

    /**
     * 修改密码
     * @param password
     * @param uid
     * @return
     */
    @Update("UPDATE USER SET PASSWORD = #{password} WHERE id=#{uid} ")
    int updatePassword(@Param("password") String password,@Param("uid") int uid);

    /**
     * 用户登录
     * @param username 账号
     * @param password 密码
     * @return
     */
    @Select("SELECT * FROM USER WHERE username = #{username} AND PASSWORD = #{password}")
    User login(@Param("username") String username, @Param("password")String password);

    /**
     * 查询用户是否存在
     * @param username
     * @return
     */
    @Select("SELECT count(*) FROM USER WHERE username = #{username}")
    int exist(@Param("username") String username);

    /**
     * 登录之后 改变token
     * @return
     */
    @Update("UPDATE USER SET tokensecrekey = #{tokensecrekey} WHERE id=#{id}  ")
    int updatetoken(@Param("id") int id,@Param("tokensecrekey") String tokensecrekey);

    /**
     * 根据用户ID 和Token查询是否为第一次登录
     * @param id
     * @param tokensecrekey
     * @return
     */
    @Select("SELECT count(*) FROM USER WHERE id = #{id} AND tokensecrekey = #{tokensecrekey}")
    int selByIdAndToken(@Param("id") int id,@Param("tokensecrekey") String tokensecrekey);


    /**
     * 用户注册
     * @return
     * @throws Exception
     */
    @Insert("INSERT INTO USER VALUES(DEFAULT,#{sex},#{username},#{password},#{name},'http://39.105.74.232:9000/user/default.jpg',#{secretkey},'开启','')")
    int regislogi(@Param("sex") String sex ,@Param("username") String username, @Param("password")String password,@Param("name")String name,@Param("secretkey")String secretkey)throws Exception;

    /**
     * 用户申诉
     * @param username
     * @param secretkey
     * @return
     */
    @Select("SELECT COUNT(*) FROM USER WHERE username = #{username} AND secretkey = #{secretkey}")
    int complain(@Param("username") String username,@Param("secretkey") String secretkey);

    /**
     * 修改密钥
     * @param secretkey
     * @param username
     * @return
     */
    @Update("UPDATE USER SET secretkey =#{secretkey}  WHERE username = #{username}")
    int updateByUUID(@Param("secretkey") String secretkey,@Param("username") String username);

    /**
     * 申诉修改密码
     * @param username
     * @param password
     * @return
     */
    @Update("UPDATE USER SET PASSWORD = #{password} WHERE username = #{username}")
    int updatePwdByname(@Param("username") String username,@Param("password") String password);

}
